/**
 * *******************************************
 * user: zmq
 * describe:    用户表数据库操作
 * created:
 * date: 2021/8/20
 * *******************************************
 */

import {pinus} from "pinus";
import Utils from "../../util/utils";
import {ICreateRole} from "../../consts/interface";

export default class UserDao {

    /**
     * 添加用户信息
     * @param {ICreateRole} roleData
     * @returns {Promise<any>}
     */
    public static addUser(roleData: ICreateRole): Promise<any> {

        let createTime = Utils.getCurTime();
        let sql = 'insert into user(uid,account,nickname,sex,level,headPng,createTime,loginTime) values(?,?,?,?,?,?,?,?);';
        let args = [roleData.uid, roleData.account, roleData.nickname, roleData.sex, roleData.level, roleData.headPng, createTime, createTime];

        // rpc调用dao服务器 操作数据库
        let serverID = pinus.app.getServerId();
        return pinus.app.rpc.dao.daoRemote.query({serverID: serverID}, sql, args);

    }

    /**
     * 根据账号获取角色信息
     * @param account
     * @returns {Promise<any>}
     */
    public static getUsersByAccount(account: string): Promise<any> {

        let sql = 'select * from user where account = ?  ';
        let args = [account];

        // rpc调用dao服务器 操作数据库
        let serverID = pinus.app.getServerId();
        return pinus.app.rpc.dao.daoRemote.query({serverID: serverID}, sql, args);
    }

    /**
     * 根据uid获取角色信息
     * @param uid
     * @returns {Promise<any>}
     */
    public static getUsersByUid(uid: number): Promise<any> {

        let sql = 'select * from user where uid = ?  ';
        let args = [uid];

        // rpc调用dao服务器 操作数据库
        let serverID = pinus.app.getServerId();
        return pinus.app.rpc.dao.daoRemote.query({serverID: serverID}, sql, args);
    }

    /**
     * 获取当前区的所有玩家昵称
     */
    public static getAllNickname() {

        let sql = 'select nickname from user ';
        let args = [];

        // rpc调用dao服务器 操作数据库
        let serverID = pinus.app.getServerId();
        return pinus.app.rpc.dao.daoRemote.query({serverID: serverID}, sql, args);
    }

    /**
     * 获取当前区的所有玩家昵称
     */
    public static getGmMap() {

        let sql = 'select * from gmmap ';
        let args = [];

        // rpc调用dao服务器 操作数据库
        let serverID = pinus.app.getServerId();
        return pinus.app.rpc.dao.daoRemote.doServerQuery({serverID: serverID}, sql, args);
    }
}